<template>
  <view class="tab-bar">
    <view class="tab-bar-border"></view>
    <view v-for="(item, index) in list" :key="index" class="tab-bar-item" @click="switchTab(item)">
      <image :src="selected === parseInt(item.index) ? item.selectedIconPath : item.iconPath"></image>
      <view :style="{ color: selected === parseInt(item.index) ? selectedColor : color }">{{ item.text }}</view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'TabBar',
  data() {
    return {
      selected: 0,
      color: "#000000",
      selectedColor: "#323CDD",
      list: []
    }
  },
  created() {
    // 从缓存中获取 tabBar 配置和选中状态
    const tabBarList = uni.getStorageSync('tabBarList') || []
    const selected = uni.getStorageSync('tabBarSelected') || 0
    this.list = tabBarList
    this.selected = selected
  },
  methods: {
    switchTab(item) {
      const index = parseInt(item.index)
      if (this.selected !== index) {
        uni.switchTab({
          url: item.pagePath
        })
        this.selected = index
        // 更新选中状态到缓存
        uni.setStorageSync('tabBarSelected', index)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 96rpx;
  background: white;
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
}

.tab-bar-border {
  background-color: rgba(0, 0, 0, 0.1);
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 1rpx;
  transform: scaleY(0.5);
}

.tab-bar-item {
  flex: 1;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  
  image {
    width: 54rpx;
    height: 54rpx;
  }
  
  view {
    font-size: 20rpx;
    margin-top: 4rpx;
  }
}
</style> 